package be.xios.projectquiz.database;

import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JOptionPane;

/// <summary>
/// Deze klasse zorgt ervoor dat er een restore gedaan kan worden.
/// </summary>
/// <author>Flemming Bosch</author>
public class Restore {

	/// <summary>
	/// Deze method doet een restore van de database die meegegeven word als parameter
	/// </summary>
	/// <parameter name="host">Dit is het ip van de DB server</parameter>
	/// <parameter name="port">Dit is de poort van de DB server</parameter>
	/// <parameter name="user">Dit is de inlognaam om in te loggen op de DB</parameter>
	/// <parameter name="password">Dit is het passwoord om als root in te loggen</parameter>
	/// <parameter name="dbname">Dit is de naam waarmee een nieuwe DB aangemaakt word</parameter>
	/// <parameter name="source">Dit is het path van de source file die restored moet worden</parameter>
	/// <return>TRUE wanneer de restore succesvol verlopen is. FALSE wanneer de restore mislukt is.</return>
 	/// <author>Flemming Bosch</author>
    public boolean restoreDB(String host,String port,String user, String password,String dbname, String source) {
        try {
        	Process runtimeProcess= null;
        	Process addDb = null;
        	//DATABASE MAKEN
        	String commando1 = "resources\\mysqladmin --host="+host+" -u "+user+" -p"+password+" CREATE "+dbname;
			// COMMANDO MET VAST PATH : addDb = Runtime.getRuntime().exec("C:\\xampp\\mysql\\bin\\mysqladmin --user="+user+" --password="+password+" CREATE "+dbname);
			addDb = Runtime.getRuntime().exec(commando1);
        	int completed = addDb.waitFor();
        	
			if(completed == 0){
				JOptionPane.showMessageDialog(null, "Database "+dbname+" successfully created!");
        	}
        	else {
        		JOptionPane.showMessageDialog(null, "Could not create "+dbname);
        	}
        	
			//Als de database succesvol is aangemaakt dan restoren.
			if(completed==0){
				//RESTOREN
				String commando = "resources\\mysql "+dbname+" -u "+user+" -p"+password+" -h "+host+" < "+source;
				String wacht = '\n'+"wait 5";
				String kill ='\n'+"taskkill /f /im cmd.exe /t";
	        	File f = new File("restore.bat");
	        	FileOutputStream fos = new FileOutputStream(f);
	        	fos.write(commando.getBytes());
	        	fos.write(wacht.getBytes());
	        	fos.write(kill.getBytes());
	        	fos.close();
	        	
	        	//.batfile runnen
	        	runtimeProcess = Runtime.getRuntime().exec("cmd /C start restore.bat");
	        	
	        	int processComplete = runtimeProcess.waitFor();
	        	System.err.println("ExitValue : "+processComplete);
	            if (processComplete == 0) {
	            	JOptionPane.showMessageDialog(null, "Backup successfully restored!");
	            	f.delete();
	            	return true; 
	            } else {
	            	JOptionPane.showMessageDialog(null, "Restore FAILED!");
	            	f.delete();
	            }
			}
        }
        catch (Exception ex) {
           ex.printStackTrace();
        }
        return false;
    }
}